package com.superbytecode.cloud.safety.ratelimit.exception;

import com.superbytecode.cloud.common.exception.ErrorCodeEnum;
import com.superbytecode.cloud.common.exception.IErrorCode;
import com.superbytecode.cloud.common.result.ResultWrapper;
import com.superbytecode.cloud.common.utils.http.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: beacon-cloud
 * @Package: com.superbytecode.cloud.safety.ratelimit.exception
 * @ClassName: RateLimitExceptionHandler
 * @Description: [限流全局异常帮助类]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/11/13 16:33
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Slf4j
@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
public class RateLimitExceptionHandler {

    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(RateLimitException.class)
    public ResultWrapper<Void> handleException(RateLimitException rateLimitException) {
        log.error("限流接口异常，uri：{}", HttpRequestUtil.obtainRequestInfo(), rateLimitException);
        return ResultWrapper.failure(ErrorCodeEnum.BAD_REQUEST, rateLimitException.getMessage());
    }
}